package com.xbongbong.paas.util;

import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 *
 * @author 魏荣杰
 * @date 2019/11/23 4:43 下午
 * @since v4.6.0
 * @version v4.6.0
 */
public class ExcelImportDataFormatter extends DataFormatter {
    private static final Logger LOG = LoggerFactory.getLogger(ExcelImportDataFormatter.class);

    @Override
    public String formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) {

        // Is it a date?
        if(DateUtil.isADateFormat(formatIndex,formatString)) {
            if(DateUtil.isValidExcelDate(value)) {
                Date d = DateUtil.getJavaDate(value, use1904Windowing);
                try {
                    return String.valueOf(DateTimeUtil.getInt(d));
                } catch (Exception e) {
                    LOG.error("com.xbongbong.paas.util.ExcelImportDataFormatter.formatRawCellContents error: ", e);
                }
            }

        }

        return super.formatRawCellContents(value, formatIndex, formatString, use1904Windowing);
    }
}
